<?php
if (!defined('IN_WONDERSHARE'))
{
	die('Hacking attempt');
}
require('./classes/module/module_article.php');

class ArticleControl
{
	var $article_m, $ret_text = array(), $ret_num = 0, $times = 0, $foreach_num = 0, $all_num = 0;
	function __construct()
	{
	
		$this->article_m = new ArticleModule();
	}
	/**
	* 得到所有不删除的文章
	* @param   int        $w_type         1为wondershare网站，2为aimersoft网站，3为anypdftools.com网站，4为applemacvideo.com网站，5为www.wondershare.jp网站，6为www.wondershare.de网站
	* @param   int         $ar_id    	                        文章id
	* @param   string      $related_article    文章组合id
	* @param   int         $parent_id           父类 id
	* @return  array
	*/
	function  get_related_article($w_type,$ar_id,$related_article = '',$parent_id = 0)
	{
		$str = '';
		$str = empty($ar_id)?'':' AND ar_id!= '.$ar_id;
		$str .= empty($parent_id)?'':' AND ar_cat_id = '.$parent_id;
		$str .= empty($w_type)?'':' AND web_type = '.$w_type;
		$sql = "SELECT ar_id , ar_title FROM topic_article  WHERE is_delete = 0  $str ";
		
		$article = $this->article_m->get_article_info($sql,'related');
		
		$select = '';
		if(empty($related_article))
		{
			for($i = 0 ;$i<count($article);$i++)
			{ 
				$select.='<input type="checkbox" name="article[]" value='.$article[$i]['ar_id'].' />'.
				htmlspecialchars($article[$i]['ar_title']).'<br>';
			}
		}else
		{
			$related_arr = explode(',',$related_article);
			for($i = 0 ;$i<count($article);$i++)
			{ 
				$fage = 1;
				foreach ($related_arr as $key=>$value)	
				{
					if($value == $article[$i]['ar_id'])
					{
						$fage = 0;
						$select.='<input type="checkbox" name="article[]" value='.$article[$i]['ar_id'].' checked="checked" />'.
						htmlspecialchars($article[$i]['ar_title']).' <br>';
					}
				}
				if($fage == 1)
				{
				
					$select.='<input type="checkbox" name="article[]" value='.$article[$i]['ar_id'].'  />'.htmlspecialchars($article[$i]['ar_title']).
					' <br>';
				}
			
			}
		}
		
		return  $select;
	}

	/**
	* 得到某个网站的所有tag 标记
	* @param   int        $w_type         1为wondershare网站，2为aimersoft网站，3为anypdftools.com网站，4为applemacvideo.com网站，5为www.wondershare.jp网站，6为www.wondershare.de网站
	* @return  array
	*/
	function  get_all_tag($w_type)
	{
		$sql = 'SELECT tag_id, tag_name FROM tag WHERE is_delete = 0 AND tag_web =  '.$w_type.'  ORDER BY  tag_name ASC   ';
		return $this->article_m->get_article_info($sql);
	}
	/**
	* 得到某个网站的所有产品列表 
	* @param   int        $w_type         1为wondershare网站，2为aimersoft网站，3为anypdftools.com网站，4为applemacvideo.com网站，5为www.wondershare.jp网站，6为www.wondershare.de网站
	* @return  array
	*/
	function  get_all_products($w_type)
	{
		$sql = 'SELECT p.product_id, p.product_name FROM won_product as won left join product as p on 
		won.won_product_id=p.product_id WHERE p.product_delete = 0 AND won.web_type =  '.$w_type.'  ORDER BY  p.product_name ASC   ';
		return $this->article_m->get_article_info($sql);
	}
	/**
	* 得到列表或者其它的文章信息
	* @param   int        $w_type         1为wondershare网站，2为aimersoft网站，3为anypdftools.com网站，
	* 4为applemacvideo.com网站，5为www.wondershare.jp网站，6为www.wondershare.de网站
	* @param   int         $type           类型（list为列表 不带搜索 而 list_s 在列表 带有搜索）
	* @param   int         $start          从那行取数据
	* @param   int         $pagesize       取多少条记录
	* @param   int         $cat_id         类别id
	* @param   int         $is_recommend   是否推荐
	* @param   int         $is_hot    	  是否热门
	* @param   string      $ar_title    	  文章部分标题
	* $param   int		$ar_id			文章id
	* @param   string		$update_user	修改人
	* @param	 int		$pub_status		是否生成html
	* @param	 string 	$sortby			排序字段
	* @param	 string		$sort			排序类型
	* @return  array
	*/
	function  get_article_info($w_type,$type,$start = 0,$pagesize = 10,$del_list = 0,$tag_id = 0,$cat_id = 0,$sumbit_type = '',$is_recommend = 0,
	$is_hot = 0,$keyword_type='ar_title',$keyword='', $ar_id = 0, $update_user = '', $pub_status = 0, $sortby = '', $sort = '')
	{
		$where_start = $tag_id == 0?'':' AND  FIND_IN_SET('.$tag_id.',ar_tag_id) ';
		$where_start.= $w_type == 0?'':' AND web_type = '.$w_type;//查询属于那个网站的
		$where_start.= $ar_id == 0?'':' AND ar_id = '.$ar_id;
		$where_start.= $update_user == '' ? '' : ' AND update_user = \''.$update_user.'\'';
		$where_start.= $pub_status == 0 ? '' : ( $pub_status == 1 ? ' AND pub_status = 1' : ' AND pub_status = 0');
		
		$sort_str = $sortby == ''?'ORDER BY ar_id DESC ':' ORDER BY '.$sortby .' '.$sort;

		if($type == 'list')
		{
			$sql = "SELECT ar_id,ar_title,ar_image,is_recommend,state,click_count,author_id,is_hot,is_delete,add_user,add_time ,update_user ,update_time,pub_time,pub_status,template_url, aut.author FROM topic_article AS art
					LEFT JOIN author AS aut ON aut.aid = art.author_id
					WHERE is_delete = $del_list $where_start $sort_str LIMIT $start,$pagesize";
		}elseif($type == 'list_s')
		{	
			$where_str1 = $this->handle_post($cat_id,$is_recommend,$pub_status,$is_hot,$keyword_type,$keyword,'ar_cat_id',$sumbit_type);
			$where_str2 = $this->handle_post($cat_id,$is_recommend,$pub_status,$is_hot,$keyword_type,$keyword,'ar_vicecat_id',$sumbit_type);
			$sql = "(
				SELECT ar_id,ar_title,ar_image,is_recommend,state,click_count,author_id,is_hot,is_delete ,add_user,add_time ,update_user ,update_time ,pub_time,pub_status ,template_url, aut.author FROM topic_article AS art
				LEFT JOIN author AS aut ON aut.aid = art.author_id
				WHERE is_delete = $del_list $where_start $where_str1
			) UNION (
				SELECT ar_id,ar_title,ar_image,is_recommend,state,click_count,author_id,is_hot,is_delete ,add_user ,add_time ,update_user ,update_time ,pub_time,pub_status ,template_url, aut.author FROM topic_article AS art
				LEFT JOIN author AS aut ON aut.aid = art.author_id
				WHERE is_delete = $del_list $where_start $where_str2
			) 
			$sort_str LIMIT $start, $pagesize";
		}
		return $this->article_m->get_article_info($sql);
	}
	/**
	* ajax改变值
	* @param   int        $w_type         1为wondershare网站，2为aimersoft网站，3为anypdftools.com网站，
	* 4为applemacvideo.com网站，5为www.wondershare.jp网站，6为www.wondershare.de网站
	* @param   int      $ar_id           文章id
	* @param   int      $change_value    值
	* @param   int      $field           要改变的数据字段
	* @return  int
	*/
	function  change_info($w_type,$ar_id,$change_value,$field,$type)
	{
		if($type == 'update_cat')
		{
			$sql = "UPDATE topic_article SET $field ='$change_value' , update_time = '".$_SERVER['REQUEST_TIME']."',
			update_user ='".$_SESSION['AName']."',modify_status = 1  WHERE ar_id = $ar_id ";
			return $this->article_m->change_info($sql);
		
		}elseif($type == 'select_cat')
		{
			$where = $ar_id == 0?'':' AND ar_id  !=  '.$ar_id;
			$where .= $w_type == 0?'':' AND web_type  =  '.$w_type;
			$where .= ' AND is_delete  = 0 ';
			$sql = "SELECT ar_id  FROM topic_article  WHERE ar_title  = '".$change_value."' $where  ";
			$query = $GLOBALS['db']->query($sql);
			return $GLOBALS['db']->num_rows($query);
		}
	}
	/**
	* 批量删除
	*
	* @param   string      $list_id       以逗号隔开的文章id字符串
	* @return  int
	*/
	function delete_article($list_id,$type)
	{
		if($type == 'delete')
		{
			$sql = "UPDATE  topic_article  SET is_delete = 1 , update_time = '".$_SERVER['REQUEST_TIME']."',
			update_user ='".$_SESSION['AName']."'  WHERE ar_id IN ($list_id) ";
		}
		else 
		{
			$sql = "UPDATE  topic_article  SET is_delete = 0  , update_time = '".$_SERVER['REQUEST_TIME']."',
			update_user ='".$_SESSION['AName']."' WHERE ar_id IN ($list_id) ";
		}
		
		return $this->article_m->change_info($sql);
	}
	/**
	* 得到子分类字符串
	*
	* @param   string      $list_id       以逗号隔开的文章id字符串
	* @return  int
	*/
	function get_sub_category($cat_id)
	{
		$sql = "SELECT cat_id   FROM  category  WHERE parent_path  LIKE '%".$cat_id."%' ";
		$str = $this->article_m->get_sub_category($sql);
		return $str;
	}
	/**
	* 得到数据提交条件
	*
	* @param   int        $cat_id          类别id
	* @param   string     $is_recommend    是否推荐
	* @param   string     $pub_status    	 是否发布
	* @param   string     $is_hot      	  是否热门
	* @param   string     $ar_title        文章部分标题名称
	* @return  string
	*/
	function handle_post($cat_id,$is_recommend,$pub_status,$is_hot,$keyword_type,$keyword,$type = '',$sumbit_type= '')
	{ 
		$where_str = $sub_str = '';
		if(!empty($cat_id))
		{
			if(empty($sumbit_type))//当不是从类别列表页链接而是从文章本页
			{	
				$sub_str= $this->get_sub_category((int)$cat_id);
			}
			if(empty($sub_str))
			{
				if($type == 'ar_cat_id')
				$where_str.=' AND ar_cat_id =  '.$cat_id;
				else 
				$where_str.=' AND ar_cat_id !=  '.$cat_id.' AND FIND_IN_SET( '.$cat_id.', ar_vicecat_id )  ';
			}else
			{
				$sub_str = $sub_str.$cat_id;
				if($type == 'ar_cat_id')
					$where_str.=' AND  FIND_IN_SET(ar_cat_id,"'.$sub_str.'")';
				else 
					$where_str.=' AND  FIND_IN_SET(ar_vicecat_id,"'.$sub_str.'")  ';
			}
		}
		if(!empty($is_recommend))
		{
			$is_recommend = (int)$is_recommend == 2?0:(int)$is_recommend ;
			$where_str.=' AND is_recommend = '.$is_recommend;
		}
		if(!empty($pub_status))
		{
			$pub_status = (int)$pub_status == 2?0:(int)$pub_status ;
			$where_str.=' AND pub_status = '.$pub_status;
		}
		if(!empty($is_hot))
		{
			$is_hot = (int)$is_hot == 2?0:(int)$is_hot ;
			$where_str.=' AND is_hot = '.$is_hot;
		}
		if(!empty($ar_title))
		{
			$ar_title = trim($ar_title);
			$where_str.=" AND ar_title LIKE '%".$ar_title."%' ";
		}
		if(!empty($keyword))
		{
			$where_str.=" AND {$keyword_type} LIKE '%".trim($keyword)."%' ";
		}
		return  $where_str;
	}
	/**
	* 得到数据提交条件返回url参数
	* @param   int        $w_type         1为wondershare网站，2为aimersoft网站，3为anypdftools.com网站，4为applemacvideo.com网站，5为www.wondershare.jp网站，6为www.wondershare.de网站
	* @param   int        $cat_id          类别id
	* @param   string     $is_recommend    是否推荐
	* @param   string     $state    		  审核状态
	* @param   string     $is_hot      	  是否热门
	* @param   string     $is_delete   	  是否删除
	* @param   string     $ar_title        文章部分标题名称
	* @return  string
	*/
	function get_url_par($w_type,$cat_id,$is_recommend,$state,$is_hot,$keyword_type,$keyword,$sumbit_type,$del_list,$sortby,$sort)
	{ 
		$url_str = '&url_page=1';
		if(empty($sumbit_type))
		{
			if(!empty($cat_id))
			{
				$url_str.='&cat_id='.$cat_id;
			}  
		}
		if(!empty($w_type))
		{
			$url_str.='&web_type='.$w_type;
		}
		if(!empty($is_recommend))
		{
			$url_str.='&is_recommend='.(int)$is_recommend;
		}
		if(!empty($state))
		{
			$url_str.='&pub_status='.(int)$state;
		}
		if(!empty($is_hot))
		{
		
			$url_str.='&is_hot='.(int)$is_hot;
		}
		if(!empty($keyword))
		{
			$keyword = urlencode(str_replace('\\','',$keyword));
			$url_str .= '&keyword='.$keyword.'&keyword_type='.$keyword_type ;
		}
		
		//note 对函数那么多参数表示很无奈，只有这么加了，该参数来自文章“编辑”中的“相关联文章”搜索
		if( !empty( $_REQUEST['forselect']) ){
			$url_str .= '&forselect='. $_REQUEST['forselect'].'&selected='.$_REQUEST['selected'].'&limit='. $_REQUEST['limit'].'&hide_ar_id='.$_REQUEST['hide_ar_id'];
		}
		
		$url_str .= $del_list == 0 ?'':'&del_list='.$del_list;
		$url_str .= $sortby == '' ? '':'&sortby='.$sortby.'&sort='.$sort; 
		return  $url_str;
	}
	/**
	* 根据id得到文章信息
	*
	* @param   int        $ar_id          文章 id
	* @return  array
	*/
	function  get_article_id_info($ar_id)
	{
		$sql = "SELECT ar_id,ar_title,ar_image,ar_subtitle,ar_introduction,ar_content,ar_vicecat_id,ar_cat_id,ar_tag_id,
		is_recommend,click_count, author_id, is_hot,is_delete,related_article,product_related,related_pro_bottom FROM topic_article WHERE ar_id = $ar_id";
		return $this->article_m->get_article_info($sql);
	}
	/**
	* 得到分页相关信息
	* @param   int        $w_type         1为wondershare网站，2为aimersoft网站，3为anypdftools.com网站，4为applemacvideo.com网站，5为www.wondershare.jp网站，6为www.wondershare.de网站
	* @param   int        $page           页码
	* @param   int		$page_size		取多少条记录
	* @param   int        $type           为1代表没有条件查询 为2代表有条件查询
	* @param   int        $tag_id         标签id
	* @param   int        $cat_id         类别id
	* @param   int        $is_recommend   是否推荐
	* @param   int        $is_hot         是否热门
	* @param   string     $ar_title       文章部分标题名称
	* $param   int		$ar_id			文章id
	* @param   string		$update_user	修改人
	* @param	 int		$pub_status		是否生成html
	* @param	 string 	$sortby			排序字段
	* @param	 string		$sort			排序类型
	* @return  array
	*/
	function get_url_info($w_type,$page,$pagesize,$type,$del_list = 0,$tag_id = 0,$cat_id = 0,$sumbit_type = '',$is_recommend = 0,
	$is_hot = 0,$keyword_type='ar_title',$keyword='', $ar_id = 0, $update_user = '', $pub_status = '', $sortby = '', $sort = '')
	{
		$url_str = '';
		$where_start = $tag_id == 0?'':' AND FIND_IN_SET('.$tag_id.',ar_tag_id) ';
		$where_start.= $w_type == 0?'':' AND web_type = '.$w_type;//查询属于那个网站的
		$where_start.= $ar_id == 0?'':' AND ar_id = '.$ar_id;
		$where_start.= $update_user == ''?'':' AND update_user = \''.$update_user.'\'';
		$where_start.= $pub_status == 0 ? '' : ( $pub_status == 1 ? ' AND pub_status = 1' : ' AND pub_status = 0');
		
		$sort_str = $sortby == ''?'':' ORDER BY '.$sortby .' '.$sort;
		
		if($type == 1)
		{
			$sql_p = "SELECT COUNT(*) AS total FROM topic_article WHERE is_delete = $del_list $where_start";
			$page_info = get_page_info($sql_p,$pagesize,$page);
		}else
		{
			$where_str1 = $this->handle_post($cat_id,$is_recommend,$pub_status,$is_hot,$keyword_type,$keyword,'ar_cat_id',$sumbit_type);
			$where_str2 = $this->handle_post($cat_id,$is_recommend,$pub_status,$is_hot,$keyword_type,$keyword,'ar_vicecat_id',$sumbit_type);
			$url_str = $this->get_url_par($w_type,$cat_id,$is_recommend,$pub_status,$is_hot,$keyword_type,$keyword,$sumbit_type,$del_list,$sortby,$sort);
			$sql_p = "SELECT * FROM (SELECT ar_id".($sortby ? ','.$sortby : '')." FROM topic_article WHERE is_delete = $del_list  $where_start  $where_str1
			UNION SELECT ar_id AS total ".($sortby ? ','.$sortby : '')." FROM topic_article WHERE is_delete = $del_list  $where_start  $where_str2) AS a $sort_str";
			
			$page_info = get_page_info($sql_p,$pagesize,$page,'count_union');
		}
		
		$page_url = get_page_url($page,$page_info['page_num'],$url_str);
		
		return  array('page_info'=>$page_info,'page_url'=>$page_url);
	}
	/**
	* 增加文章或者修改
	* @param   array        $post_arr   提交数据（数组）
	* @return  int
	*/
	function  operate_article($post_arr) {
		
		if ($post_arr['act_type'] == 'add') {
			
			$sql = "INSERT INTO topic_article (ar_vicecat_id ,ar_image,ar_title,ar_subtitle,ar_cat_id,is_recommend, 
					is_hot,click_count, author_id, ar_introduction,ar_content,add_time,add_user,ar_tag_id,related_article,update_time,update_user,web_type,product_related
					,related_pro_bottom) 
					VALUES 
					('{$post_arr['other_cat_id']}','{$post_arr['ar_image']}','{$post_arr['ar_title']}',
					'{$post_arr['ar_subtitle']}', '{$post_arr['parent_id']}','{$post_arr['is_recommend']}',
					'{$post_arr['is_hot']}','{$post_arr['click_count']}', '{$post_arr['author_id']}', '{$post_arr['ar_introduction']}',
					'{$post_arr['ar_content']}' ,'{$_SERVER['REQUEST_TIME']}','{$_SESSION['AName']}', 
					'{$post_arr['ar_tag_id']}','{$post_arr['related_article']}','{$_SERVER['REQUEST_TIME']}',
					'{$_SESSION['AName']}','{$post_arr['w_type']}','{$post_arr['product_related']}','{$post_arr['related_pro_bottom']}') ";
			$GLOBALS['db']->query($sql);
			
			return $GLOBALS['db']->insert_id();
			
		} elseif ($post_arr['act_type'] == 'edit') {
			
			$sql = "UPDATE topic_article SET ar_title = '{$post_arr['ar_title']}' ,ar_subtitle = '{$post_arr['ar_subtitle']}',
					ar_introduction = '{$post_arr['ar_introduction']}',ar_content = '{$post_arr['ar_content']}',
					ar_image = '{$post_arr['ar_image']}',ar_vicecat_id ='{$post_arr['other_cat_id']}',
					ar_cat_id = '{$post_arr['parent_id']}' ,update_time = '{$_SERVER['REQUEST_TIME']}',
					update_user ='{$_SESSION['AName']}' ,is_recommend = '{$post_arr['is_recommend']}',
					related_article = '{$post_arr['related_article']}', click_count = '{$post_arr['click_count']}',author_id= '{$post_arr['author_id']}',
					is_hot = '{$post_arr['is_hot']}' , ar_tag_id = '{$post_arr['ar_tag_id']}',modify_status = 1 ,product_related='{$post_arr['product_related']}'
					,related_pro_bottom='{$post_arr['related_pro_bottom']}'
					WHERE ar_id = {$post_arr['ar_id']}";
			$affect = $this->article_m->change_info($sql);
			return empty($affect) ? 0 :  $post_arr['ar_id'];
		}
		
	
	}
	
	function getarticlebyid($pid,$type)
		{
		if($type==1)
		{
			$sql="select count(*) as total from topic_article where `is_delete`=0 and `ar_id`=".$pid;
			return $this->article_m->getarticlebyid($sql);
		}
		else 
		{
			$sqla="select * from topic_article where `is_delete`=0 and `ar_id`=".$pid;
			return $this->article_m->getarticlebyidt($sqla);
		}
	}


	/**
	* 取得所有修改人
	* @param integer $web_type 站点类型
	* @return array
	* */
	function get_update_user( $web_type ){
		$sql = "SELECT DISTINCT( update_user ) FROM topic_article WHERE web_type = {$web_type}";
		$ret = $GLOBALS['db']->getAll( $sql );
		$arr = array();
		foreach( $ret as $v ){
			if($v['update_user']) $arr[] = $v['update_user'];
		}
		return $arr;
	}


	/**
	* 将正则中特殊符号加反斜线处理 
	* */
	function preg_filter( $keyword ){
		$preg_arr = array( '/', '|', '.', '?', '+', '*', '(', ')', '[', ']', '!', '$', '^', '{', '}', ',', ';', '-' );
		
		foreach( $preg_arr as $v ){
			$keyword = str_replace( $v, '\\'.$v ,$keyword);
		}
		return $keyword;
	}

	/**
	* 整站内容搜索
	* @param int		$web_type	站点类型
	* @param string	$keyword	搜索关键字
	* @param int		$w			区分大小写：1.区分，2不区分
	* @param int		$s			搜文本或源文件：1.文本，2.源文件
	* @param int		$offset		搜索偏移量
	* @param int		$limit		搜索限制数
	* @param int		$num		符合条件结果总数
	* @param string	$ar_id_list	搜索文本时所有符合条件的文章id,用逗号隔开 
	* return array	$ret
	* */
	function get_site_search( $web_type, $keyword, $w, $s, $offset, $limit, &$num = 0, &$ar_id_list = '' ){
	
		$binary = $w == 1 ? 'BINARY' : '';
		
		$sql = "SELECT SQL_CALC_FOUND_ROWS ar_id, ar_title, ar_content FROM topic_article 
		WHERE web_type = {$web_type} AND is_delete = 0 AND ( {$binary} ar_title LIKE '%{$keyword}%' OR {$binary} ar_content LIKE '%{$keyword}%' )
		ORDER BY ar_id ASC LIMIT {$offset}, {$limit}";
		$ret = $GLOBALS['db']->getAll( $sql );
		if( $this->all_num == 0 ) $this->all_num = $GLOBALS['db']->getNum();
		
		
		if( $s == 2 ){//note 搜源文件
		$num = $this->all_num;
		return $ret;
		}elseif( $s == 1 ){//note 搜文本
			if( empty( $ret ) ){
				$num = 0;
				return array();
			}
			
			$i = $w == 1 ? '' : 'i';
			foreach( $ret as $v ){
				$c = strip_tags( $v['ar_content'] );
				
				$keyword_2 = $this->preg_filter( $keyword );
				
				if( preg_match_all( '|'.$keyword_2.'|'.$i, $c, $arr) ){
					if( $this->times < $limit ){			
					    $this->ret_text[] = $v;
					    $this->times++;
					}
					$this->ret_num++;
				}
				$this->foreach_num++;
				$max_ar_id = $v['ar_id'];
			}
			
			if( $this->times < $limit && $this->foreach_num < $this->all_num-1 ){
				$this->get_site_search( $web_type, $keyword, $w, $s, $offset + $limit, $limit, $a, $b );
			}
			$t = $this->get_site_search_text_num($web_type, $keyword, $w, $s );
			$num = $t['num'];
			$ar_id_list = $t['list'];
			return $this->ret_text;
		}
	
	}

	/**
	* 搜索文本时取得文本总记录数
	* @param int		$web_type	站点类型
	* @param string	$keyword	搜索关键字
	* @param int		$w			区分大小写：1.区分，2不区分
	* @return int	$num
	* */
	private function get_site_search_text_num( $web_type, $keyword, $w ){
		$binary = $w == 1 ? 'BINARY' : '';
		
		$sql = "SELECT ar_id, ar_content FROM topic_article 
		WHERE web_type = {$web_type} AND is_delete = 0 AND ( {$binary} ar_title LIKE '%{$keyword}%' OR {$binary} ar_content LIKE '%{$keyword}%' )";
		$ret = $GLOBALS['db']->getAll( $sql );
		
		$num = 0;
		$ar_id = array();
		foreach( $ret as $v ){
			$i = $w == 1 ? '' : 'i';
			$c = strip_tags( $v['ar_content'] );
			
			$keyword_2 = $this->preg_filter( $keyword );
			
			if( preg_match_all( '|'.$keyword_2.'|'.$i, $c, $arr) ){
				$num++;
				$ar_id[] = $v['ar_id'];
			}
		}
		$ar_id_list = implode(',', $ar_id);
		return array('num'=>$num,'list'=>$ar_id_list);
	}

	/**
	* 取得整站内容搜索结果的分页信息
	* @param int	$page 当前页数
	* @param int $num 总页数
	* return array
	* */
	function get_site_search_pages( $page, $num ){
		return get_page_url( $page, $num );
	}

	/**
	* 替换单个文章中的内容
	* @param int 	$ar_id		文章id
	* @param string	$keyword	替换的关键字
	* @param string	$replace	替换的内容
	* @param int		$w			区分大小写：1.区分，2不区分
	* @param int		$s			搜文本或源文件：1.文本，2.源文件
	* return boolean
	* */
	function site_replace_one( $ar_id, $keyword, $replace, $w, $s ){
		$sql = "SELECT `ar_title`, `ar_content` FROM `topic_article` WHERE `ar_id` = {$ar_id}";
		$c = $GLOBALS['db']->getOneRs( $sql );
		
		$i = $w == 1 ? '' : 'i';
		
		$keyword_2 = $this->preg_filter( $keyword );
		
		if( $s == 2 ){
			$ct = preg_replace('|'.$keyword_2.'|'.$i, $replace, $c['ar_title']);
			$cc = preg_replace('|'.$keyword_2.'|'.$i, $replace, $c['ar_content']);
		}elseif( $s == 1 ){
			$ct = preg_replace('|'.$keyword_2.'|'.$i, $replace, $c['ar_title']);
			$cc = preg_replace('|(?!<[^>]*)('.$keyword_2.')(?![^<]*>)|'.$i, $replace, $c['ar_content']);//替换文本中的关键字		
		}
		
		$ct = str_replace( "'", "\'", $ct );
		$cc = str_replace( "'", "\'", $cc );
		
		$sql = "UPDATE `topic_article` SET `ar_title` = '{$ct}', `ar_content` = '{$cc}' WHERE `ar_id` = {$ar_id}";
		return $GLOBALS['db']->query( $sql );
	}

	/**
	* 整站替换文章中的内容
	* @param int 	$web_type	站点类型
	* @param string	$keyword	替换的关键字
	* @param string	$replace	替换的内容
	* @param int		$w			区分大小写：1.区分，2不区分
	* @param int		$s			搜文本或源文件：1.文本，2.源文件
	* @param string	$ar_id_list	搜索文本时所有符合条件的文章id,用逗号隔开 
	* return boolean
	* */
	function site_replace_all( $web_type, $keyword, $replace, $w, $s, $ar_id_list = '' ){
	
		if( $s == 2 ){
			$binary = $w == 1 ? 'BINARY' : '';  	
			$sql = "SELECT `ar_id`, `ar_content` FROM `topic_article` WHERE web_type = {$web_type} AND is_delete =0 AND ( {$binary} ar_title LIKE '%{$keyword}%' OR {$binary} ar_content LIKE '%{$keyword}%' )";
			$ret = $GLOBALS['db']->getAll( $sql );
			foreach( $ret as $v ){	
				$this->site_replace_one( $v['ar_id'], $keyword, $replace, $w, $s );
			}
		}elseif( $s == 1 ){
			$arr = explode(',', $ar_id_list );
			if( empty( $arr ) ) return false;
			foreach( $arr as $v ){
				$this->site_replace_one( $v, $keyword, $replace, $w, $s );
			}
		}
		return true;
	}
	
	/**
    * 修改与发布模板操作
    * @param   int      $product_id  公共产品id
    * @param   string    $location  链接
    * @param   int      $is_ajax  是否是ajax请求
    * @return  string
    */
    function updateTemplate($ar_id = 0,  $w_type = '', $is_ajax =0 , $atc = '' ) {
        
 
			$update_arr = array('ar_id' => $ar_id,
								'w_type' => (int) $w_type,
								'operation_type' => 'article',
								'location' => '/wondershare/article.php?action=index&act=edit&ar_id='.$ar_id.'&w_type='.$w_type,
			 );
			 
			 if($atc == 'edit' && empty($is_ajax)) {
			 	
			 		echo '<script type="text/javascript">
								var ret = window.confirm("修改成功，是否生成其关联页面？\n\n确定：生成，取消：不生成");
								if( ret )
								{
									 window.location.href="/wondershare/template.php?action=updateTemplate&type=pub&'.http_build_query($update_arr).'";
								}else
								{
									 window.location.href="/wondershare/template.php?action=updateTemplate&type=update&'.http_build_query($update_arr).'";
								}
								</script>';
			 } else {
			 	
				if (!empty($is_ajax)) {
					
				 	$update_arr['is_ajax'] = $is_ajax;
				 	
					file_get_contents("http://cms.wondershare.com/wondershare/template.php?action=updateTemplate&type=update&".
										http_build_query($update_arr));
				
				}
			 }

    }


	

}
?>